<?php
	class RepostModel extends Model{
		public $repo;
		function repost($re){
			$re['time']=time();
			$re['ip']=$_SERVER['REMOTE_ADDR'];
			$re['uid']=$_SESSION['uid'];
			$re['pid']=$re['pid'];//
			$re['text']=$re['text'];
			$count=$this->where("pid='".$re['pid']."'")->count();
			$re['num']=$count+1;
			$rid=$this->add($re);
			D('Post')->where("pid='".$re['pid']."'")->setInc('renum');
			$re['rid']=$rid;
			if($re['rrid']){//确定是否为反馈ID
				$addtx['pid']=$re['pid'];
				$addtx['rid_1']=$re['rrid'];//被回复id
				$addtx['uid']=$this->riduid($re['rrid']);
				$addtx['rid_2']=$re['rid'];
				$addtx['num']=$re['num'];
				D('Tx')->addtx($addtx);

			}
			return $re;
		}
		function riduid($rid){
			$bk=$this->find($rid);
			if($bk['uid']){
				return $bk['uid'];
			}

		}
		function re($pid){
			$pid=(int)$pid;
			$pre=$this->where("pid='".$pid."'")->order('time desc')->select();
			//var_dump($pre);
			foreach ($pre as $key => $value) {
				$this->repo="";
				$this->re_add($value);
				$all[$key]=$this->repo;
				$all[$key]=array_reverse($all[$key]);
			}
			//var_dump($all);
			return $all;

		}
		function re_add($ar){
			$this->repo[]=$ar;
			$re=$ar['rrid'];
			if($re){
				$mx=$this->find($re);
				$this->re_add($mx);
			}else{
				return $this->repo;
				 
			}
		}

		function rere($rid){
			$rid=(int)$rid;
			$repost=$this->find($rid);
			$this->repo="";
			$this->re_add($repost);
			$a=array_reverse($this->repo);
			return $a;

		}
	}





/*
倒叙 每个！
array(4) {
 [0]=> array(1) { [0]=> array(8) { ["rid"]=> string(2) "81" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(1) "0" ["num"]=> string(2) "12" ["time"]=> string(10) "1471118521" ["text"]=> string(1) "2" ["ip"]=> string(3) "127" } } 
 [1]=> array(3) { 
 	[0]=> array(8) { ["rid"]=> string(2) "80" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(2) "79" ["num"]=> string(2) "11" ["time"]=> string(10) "1471118324" ["text"]=> string(1) "1" ["ip"]=> string(3) "127" } 
 	[1]=> array(8) { ["rid"]=> string(2) "79" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(2) "78" ["num"]=> string(2) "10" ["time"]=> string(10) "1471118134" ["text"]=> string(1) "3" ["ip"]=> string(3) "127" } 
 	[2]=> array(8) { ["rid"]=> string(2) "78" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(1) "0" ["num"]=> string(1) "9" ["time"]=> string(10) "1471118120" ["text"]=> string(3) "ccc" ["ip"]=> string(3) "127" }
 	 } 
 [2]=> array(2) { 
 	[0]=> array(8) { ["rid"]=> string(2) "79" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(2) "78" ["num"]=> string(2) "10" ["time"]=> string(10) "1471118134" ["text"]=> string(1) "3" ["ip"]=> string(3) "127" } 
 	[1]=> array(8) { ["rid"]=> string(2) "78" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(1) "0" ["num"]=> string(1) "9" ["time"]=> string(10) "1471118120" ["text"]=> string(3) "ccc" ["ip"]=> string(3) "127" }
 	 } 
 [3]=> array(1) { [0]=> array(8) { ["rid"]=> string(2) "78" ["pid"]=> string(2) "15" ["uid"]=> string(1) "1" ["rrid"]=> string(1) "0" ["num"]=> string(1) "9" ["time"]=> string(10) "1471118120" ["text"]=> string(3) "ccc" ["ip"]=> string(3) "127" } } }

 */